package com.csyf.manager.dao;

import com.csyf.manager.entity.BalanceCount;
import com.csyf.manager.entity.SysPayment;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import tk.mybatis.mapper.common.Mapper;

import java.math.BigDecimal;

/**
 * @Author: Enzo
 * @Date: 2018/4/1 15:24
 */
public interface SysPayMentDao extends Mapper<SysPayment> {

    /**
     * 根据类型计算总支出
     *
     * @param mch_id
     * @param date
     * @param services
     * @return
     */
    @Select("SELECT IFNULL(SUM(transMoney+free),0) paymoney, count(*) paycount FROM `sys_payment` WHERE mch_id = #{mch_id} AND pay_status = 3 AND pay_time BETWEEN CONCAT(#{date},' 00:00:00') AND CONCAT(#{date},' 23:59:59') and service in (${services})")
    BalanceCount sumCaseOutByServices(@Param("mch_id") String mch_id, @Param("date") String date, @Param("services") String services);


    @Select("SELECT IFNULL(SUM(transMoney+free),0) FROM sys_payment WHERE mch_id = #{mch_id} AND pay_status IN ('1','3','4') and service = 'XFZF_DF_NO' and create_time BETWEEN CONCAT(#{date},' 00:00:00') AND CONCAT(#{date},' 23:59:59')")
    BigDecimal queryJD_DF_NO(@Param("mch_id") String mch_id, @Param("date") String date);


    @Select("SELECT IFNULL(SUM(transMoney+free),0) FROM sys_payment WHERE mch_id = #{mch_id} AND pay_status IN ('3','4') and service in (${services}) and pay_time BETWEEN CONCAT(#{date},' 00:00:00') AND CONCAT(#{date},' 23:59:59')")
    BigDecimal queryDfMoneyByService(@Param("mch_id") String mch_id, @Param("date") String date, @Param("services") String services);


    /**
     * 根据查询条件统计代付金额
     *
     * @param vo
     * @return
     */
    @SelectProvider(type = PaymentSqlProvider.class, method = "sumPaymentFee")
    BigDecimal queryTotalFeeByCondition(SysPayment vo);

    /**
     * 查询代付中的金额
     *
     * @param mch_id
     * @param services
     * @return
     */
    @Select("SELECT IFNULL(SUM(transMoney+free),0) FROM sys_payment WHERE mch_id = #{mch_id} AND pay_status = '1' and service in (${services})")
    BigDecimal queryDfingMoneyByService(@Param("mch_id") String mch_id, @Param("services") String services);

    /**
     * 查询代付数量
     */
    @Select("SELECT count(*) paycount FROM `sys_payment` WHERE mch_id = #{mch_id} AND create_time BETWEEN CONCAT(#{date},' 00:00:00') AND CONCAT(#{date},' 23:59:59') and service = 'YYT_DF_SP' ")
    int queryDfCount(@Param("mch_id") String mch_id, @Param("date") String date);
}
